package com.healthdata.service.impl;

import com.healthdata.entity.DataRow;
import com.healthdata.service.FileParseService;
import com.healthdata.service.HealthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HealthDataServiceImpl implements HealthDataService {

    @Autowired
    private FileParseService fileParseService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 假设数据存储在这个列表中
    private List<DataRow> healthData = new ArrayList<>();

    // 初始化数据（实际项目中可能从数据库或文件加载）
    public HealthDataServiceImpl() {
        // 示例数据，实际项目中应该从数据库获取
        initializeSampleData();
    }

    private void initializeSampleData() {
        // 创建一些示例数据
        List<Map<String, Object>> sampleData = new ArrayList<>();

        Map<String, Object> row1 = new HashMap<>();
        row1.put("id", 1L);
        row1.put("userId", 1001L);
        row1.put("dataType", "体重");
        row1.put("value", 68.5);
        row1.put("recordDate", "2023-06-01");
        row1.put("createTime", "2023-06-01 10:30:00");
        sampleData.add(row1);

        Map<String, Object> row2 = new HashMap<>();
        row2.put("id", 2L);
        row2.put("userId", 1001L);
        row2.put("dataType", "血压");
        row2.put("value", "120/80");
        row2.put("recordDate", "2023-06-01");
        row2.put("createTime", "2023-06-01 14:15:00");
        sampleData.add(row2);

        // 将Map转换为DataRow
        for (Map<String, Object> map : sampleData) {
            DataRow dataRow = new DataRow();
            dataRow.setValues(map);
            healthData.add(dataRow);
        }
    }

    @Override
    public List<DataRow> getAllData() {
        return healthData;
    }

    @Override
    public List<DataRow> getFilteredData(Long userId, String dataType, String startDate, String endDate) {
        List<DataRow> result = new ArrayList<>();

        for (DataRow row : healthData) {
            Map<String, Object> values = row.getValues();

            // 简单过滤实现，实际项目中可能需要更复杂的条件判断
            boolean matchUserId = userId == null || userId.equals(values.get("userId"));
            boolean matchDataType = dataType == null || dataType.equals(values.get("dataType"));

            if (matchUserId && matchDataType) {
                result.add(row);
            }
        }

        return result;
    }

    @Override
    public Page<DataRow> getPaginatedData(int page, int size, String sortBy, String direction) {
        // 计算分页范围
        int start = Math.min((int)Pageable.ofSize(size).getOffset(), healthData.size());
        int end = Math.min(start + size, healthData.size());

        // 获取分页数据
        List<DataRow> pageContent = healthData.subList(start, end);

        // 创建Page对象
        return new PageImpl<>(pageContent, Pageable.ofSize(size), healthData.size());
    }

    @Override
    public List<DataRow> getDataByTableName(String tableName) {
        String sql = "SELECT * FROM " + tableName;
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
        List<DataRow> data = new ArrayList<>();

        for (Map<String, Object> row : rows) {
            DataRow dataRow = new DataRow();
            dataRow.setValues(row);
            data.add(dataRow);
        }

        return data;
    }
}